let app = getApp();

Page({
  data: {
    currentContentId: 0,   //默认展示第一个未完成的内容
    purchaseOrderList: [],
    purchaseList:[],
    searchInput:"",
  },
  onLoad() {
    var that = this;
    var date = new Date();
    that.setData({
      purchaseOrderList: [],
    })
    setTimeout(() => {
      this.setData({
        activeIndexDefault: 1
      })
    }, 2000)
    // this.getPurchaseOrderList('');   
  },

  onShow() {
    var that = this;
    that.setData({
      purchaseList: [],
      purchaseOrderList: [],
    })
    console.log("222222222222222222222222222222222222222222");
    that.getPurchaseOrderList('');
  },
  

    // 扫码
    onSearchShop() {
      const that = this;
      dd.scan({
          type: 'qr',
          success: (res) => {
              console.log("扫码结果", res.code);
              that.onSearchInput({ detail: { value: res.code } }); // 调用输入框值改变事件，并传入扫码结果
          },
          fail: (err) => {
              // 扫码失败后的回调函数，可以根据实际需求进行处理
          }
      });
  },
    // 点击了哪个状态
    toApprovalHall(e) {
      const contentId = e.currentTarget.dataset.id; // 获取点击的内容的id
      console.log("我点击了第几个=====>",contentId);
      var that = this;
      that.setData({
        currentContentId: contentId, // 更新当前展示的内容id
        page:1,
        purchaseOrderList:[],
        dowm:true,
      });
  
      if(contentId < 1){
        that.getPurchaseOrderList('');
      }else{
        that.getPurchaseOrderListEnd('');
      }
  
    },
  
    
  
    // 获取输入内容进行搜索
    onSearchInput(e){
      console.log("e===>>",e)
      var that = this;
      that.setData({
        searchInput:e,
        page:1,
        dowm:true,
        purchaseOrderList:[]
      })
      
      that.getPurchaseOrderList();
  
    },
  
  // 用于监听输入框值改变事件
  onSearchInput(e) {
    var that = this;
    var searchTerm = e.detail.value.trim(); // 获取输入框的搜索内容并去除首尾空格
    console.log("输入框用来搜索的内容", searchTerm);
  
    // 清除之前的搜索定时器
    clearTimeout(that.data.searchTimer);
  
    // 设置一个新的定时器，在用户停止输入300毫秒后执行搜索操作
    var searchTimer = setTimeout(function() {
        that.setData({
            searchTimer: null // 清空定时器
        });
        // that.getPurchaseOrderList(searchTerm); // 执行搜索操作
        // that.getPurchaseOrderListEnd(searchTerm)

        var contentId = that.data.currentContentId

        if(contentId < 1){
          that.getPurchaseOrderList(searchTerm);
        }else  {
          that.getPurchaseOrderListEnd(searchTerm)
        }



    }, 300);
  
    // 将定时器存储在数据中
    that.setData({
        searchTimer: searchTimer
    });
  },
  
  // 清除搜索框内容
  onSearchClear(e) {
    var that = this;
    that.setData({
      searchInput: '', // 清空搜索框内容
      page: 1,
      dowm: true,
      purchaseOrderList: []
    });
  },
  
    // 获取未完成数据
    getPurchaseOrderList(searchTerm) {
      console.log("进入了");
      var that = this;
  
      // 在请求开始前先清空数组
      that.setData({
        purchaseList: []  // 清空数组
      });

      var shujuKu = app.globaldata.cipher_code
      console.log("数据库==>",shujuKu);
     
      var data = JSON.stringify({
        "q": {
          "encrypted_text": shujuKu,
          "key": "66249c7f42e1e400",
          "sql": "select t1.FID as FID, t1.fbillno as 单据编号,t1.FDate as 收料日期,T3.FNAME as 供应商名称,SUM(t2.FActlandQty) AS 实到数量,COUNT(T2.FMATERIALID) AS 项数,T4.FNAME AS 收料员 from T_PUR_RECEIVE t1 inner join T_PUR_RECEIVEENTRY t2 on t2.fid=t1.fid left join T_BD_SUPPLIER_L t3 on t3.FSUPPLIERID=t1.FSUPPLIERID left join V_BD_WAREHOUSEWORKERS_L t4 on t4.FID=t1.FReceiverId where t1.fbillno like '%" + searchTerm + "%' and t1.FDocumentStatus <>'C' and t1.FDocumentStatus <>'B' group by t1.FID,t1.fbillno,t1.FDate,T3.FNAME,T4.FNAME order by t1.FID",
          "fields": [
            "FID","单据编号","收料日期","供应商名称","实到数量","项数","收料员"
          ]
        },
        "p": {
          "page_number": 1,
          "page_size": 20
        }
        })
        console.log("发起请求用到的",data);
      dd.httpRequest({
        url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
        method: 'POST',
        headers: {
          "Content-Type": "application/json"
        },
        data: data,
        dataType: 'json',
        success: function (res) {
          console.log('获取配货单未完成数据响应==>', res.data)
          if (res.data.code == 200) {
          console.log('获取配货单未完成数据响应200000==>', res.data)
            var list = res.data.data;
            var purchaseList = that.data.purchaseList;
            for(var i in list){
              purchaseList.push(list[i])
            }
            if(list.length < that.data.page_size){
              that.setData({
                dowm:false
              })
            }
            that.setData({
              purchaseList:purchaseList
            })
            console.log("数据",purchaseList);
          } else {
            console.log("配货未完成的无数据");
            // dd.showToast({
            //   type: 'fail',
            //   content: res.data.data,
            //   duration: 2000,
            // });
          }
        },
        fail: function (res) {
          // dd.alert({ content: 'fail' });
        },
      });
    },
  
    // 获取已经完成数据
    getPurchaseOrderListEnd(searchTerm) {
      console.log("进入了");
      var that = this;
      // 在请求开始前先清空数组
      that.setData({
        purchaseList: [], // 清空数组
        purchaseOrderList: []  // 清空数组
      });
      var shujuKu = app.globaldata.cipher_code
      console.log("数据库==>",shujuKu);

      var data = JSON.stringify({
        "q": {
          "encrypted_text": shujuKu,
          "key": "66249c7f42e1e400",
          "sql": "select t1.FID as FID, t1.fbillno as 单据编号,t1.FDate as 收料日期,T3.FNAME as 供应商名称,SUM(t2.FActlandQty) AS 实到数量,COUNT(T2.FMATERIALID) AS 项数,T4.FNAME AS 收料员 from T_PUR_RECEIVE t1 inner join T_PUR_RECEIVEENTRY t2 on t2.fid=t1.fid left join T_BD_SUPPLIER_L t3 on t3.FSUPPLIERID=t1.FSUPPLIERID left join V_BD_WAREHOUSEWORKERS_L t4 on t4.FID=t1.FReceiverId where t1.fbillno like '%" + searchTerm + "%' and t1.FDocumentStatus ='C' group by t1.FID,t1.fbillno,t1.FDate,T3.FNAME,T4.FNAME order by t1.FID",
          "fields": [
            "FID","单据编号","收料日期","供应商名称","实到数量","项数","收料员"
          ]
        },
        "p": {
          "page_number": 1,
          "page_size": 20
        }
        })
  
      dd.httpRequest({
        url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
        method: 'POST',
        headers: {
          "Content-Type": "application/json"
        },
        data: data,
        dataType: 'json',
        success: function (res) {
          console.log('获取配货单未完成数据响应==>', res.data)
          if (res.data.code == 200) {
          console.log('获取配货单未完成数据响应200000==>', res.data)
            var list = res.data.data;
            var purchaseOrderList = that.data.purchaseOrderList;
            for(var i in list){
              purchaseOrderList.push(list[i])
            }
            if(list.length < that.data.page_size){
              that.setData({
                dowm:false
              })
            }
            that.setData({
              purchaseOrderList:purchaseOrderList
            })
            console.log("数据",purchaseOrderList);
          } else {
            console.log("配货未完成的无数据");
            // dd.showToast({
            //   type: 'fail',
            //   content: res.data.data,
            //   duration: 2000,
            // });
          }
        },
        fail: function (res) {
          // dd.alert({ content: 'fail' });
        },
      });
    },

  // 已完成点击查看详情
  gobuyStorageTwoy(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    console.log("已完成的item",item);
    dd.navigateTo({
      url: '/pages/buyStorageTwoy/buyStorageTwoy?item='+JSON.stringify(item),

    })
  },
  
  // 去入库查看详情
  gobuyStorageTwo(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    console.log("已完成的item",item);
    dd.navigateTo({
      url: '/pages/buyStorageTwo/buyStorageTwo?item='+JSON.stringify(item),
    })
  },

  // 下推采购入库单
  // gobuyStorageTwo(e) {
  //   var that = this
  //   var fidpc = [];
  //   const item = e.currentTarget.dataset.item; // 获取传递的数据item
  //   const fidp = item.FID.toString(); // 从item中提取fid并转换为字符串
  //   fidpc.push(fidp); // 将fidp添加到fidpc数组中
  //   console.log('点击的item的fid：', fidp);
  //   console.log('fidpc数组：', fidpc);
  //   // var hangidList = that.data.hangidList.map(String);
  //   // console.log("下推接口调用行id", hangidList);
  //   var data = JSON.stringify(
  //     {
  //       "push": {
  //         "formid": "PUR_ReceiveBill", 
  //         "RuleId": "d9ea969c-adad-4fcc-89c6-a5fee0fcd063",
  //         "Ids": fidpc,
  //         "EntryIds": [],
  //         "IsDraftWhenSaveFail": false
  //       },
  //       "login": {
  //         "acctID": "65af0c304f878e",
  //         "url": "cloud.reece-sewing.com:8090",
  //         "username": "demo",
  //         "password": "zz111111"
  //       }
  //     }
  //   )

  //   my.showLoading({
  //     content: '',
  //   })
  //   console.log("点击提交创建的data",data);
  
  //   dd.httpRequest({
  //     url: 'http://cloud.reece-sewing.com:9351/king_push_down',
  //     method: 'POST',
  //     headers: {
  //       "Content-Type": "application/json"
  //     },
  //     data: data,
  //     dataType: 'json',
  //     success: function (res) {
  //       if(res.data.code == 200){
  //       // console.log("下推按钮的success响应200======》",res);
  //       console.log('下推响应==>',JSON.parse(res.data.data))
  //       var dataxiatui = JSON.parse(res.data.data)
  //       console.log("ttttttttttt",dataxiatui);
  //       console.log("idddddddddddddddd====>",dataxiatui.Result.ResponseStatus.SuccessEntitys[0].Id);

  //       dd.confirm({
  //         title: '温馨提示',
  //         content: '是否确认下推',
  //         confirmButtonText: '确定下推',
  //         cancelButtonText: '暂不需要',
  //         success: (result) => {
  //           console.log(result);
  //           if (result.confirm) {
  //             dd.navigateTo({
  //               // url: '/pages/login/login?stop=1'
  //               url: '/pages/buyStorageTwo/buyStorageTwo?id=' + dataxiatui.Result.ResponseStatus.SuccessEntitys[0].Id
  //             })

  //             my.hideLoading(); // 隐藏 loading 提示框
  //           }else {
  //             // 用户点击"暂不需要"的逻辑
  //             my.hideLoading(); // 取消 loading 提示框
  //           }
    
  //         },
  //       });
  //       // 1

  //       my.hideLoading();
  //       }else{
  //         my.hideLoading(); // 隐藏 loading 提示框

        
  //       dd.alert({ content: errorMessage });
       
  //         console.log("采购入库单修改提交按钮的code!=200响应",res);
  //       }
     
  //     },
  //     fail: function (res) {
  //       // dd.alert({ content: 'fail' });
  //       // dd.showToast({
  //       //   type: 'fail',
  //       //   content: res.errorMessage,
  //       //   duration: 2000,
  //       // });
  //       console.log("提交按钮的fail响应",res);
  //       my.hideLoading();
  //     },
  //     complete: function (res) {
  //       // console.log("提交按钮的complete响应",res);

  //       // dd.alert({ content: 'complete' });
  //       my.hideLoading();
  //     }
  //   });
  // },



});
